package com.yixin.hospital.controller;

import cn.hutool.core.lang.tree.Tree;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.drugType.DrugTypeAddReqDTO;
import com.yixin.hospital.entity.dto.req.drugType.DrugTypeEditReqDTO;
import com.yixin.hospital.service.DrugTypeService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/drug-type")
@Api(value = "药品类别管理", tags = "药品类别管理")
public class DrugTypeController {

    @Resource
    private DrugTypeService drugTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "药品类别列表（树状）")
    public ApiResult<List<Tree<Integer>>> list() {
        return drugTypeService.treeList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加药品类别")
    public ApiResult add(@Valid @RequestBody DrugTypeAddReqDTO drugTypeAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugTypeService.add(drugTypeAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑药品类别")
    public ApiResult edit(@Valid @RequestBody DrugTypeEditReqDTO drugTypeEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugTypeService.edit(drugTypeEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除药品类别")
    public ApiResult remove(@PathVariable int id) {
        drugTypeService.removeById(id);
        return ApiResult.success();
    }
}
